<template>
  <div class="login-container">
    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on" label-position="left">
      <el-card class="box-card">
        <div class="title-container">
          <h3 class="title">Feijiu管理后台</h3>
        </div>
        <el-form-item prop="account" class="changestyle">
          <span class="svg-container">
            <svg-icon icon-class="user" />
          </span>
          <el-input ref="account" v-model="loginForm.account" placeholder="用户名" name="account" type="text" tabindex="1" auto-complete="on" />
        </el-form-item>

        <el-form-item prop="password" class="changestyle">
          <span class="svg-container">
            <svg-icon icon-class="password" />
          </span>
          <el-input :key="passwordType" ref="password" v-model="loginForm.password" :type="passwordType" placeholder="密码" name="password" tabindex="2" auto-complete="on" @keyup.enter.native="handleLogin" />
          <span class="show-pwd" @click="showPwd">
            <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
          </span>
        </el-form-item>
        <!--        <p class="findpassstyle" @click="dialogFormVisible = true">找回密码</p>-->
        <el-button :loading="loading" type="primary" style="width: 100%; margin-bottom: 30px" @click.native.prevent="handleLogin">登 录 </el-button>
      </el-card>
    </el-form>
    <!-- 忘记密码找回弹框 -->
    <el-dialog title="忘记密码" :visible.sync="dialogFormVisible">
      <el-form ref="dataForm" :rules="rules" :model="dataForm" status-icon label-position="left">
        <el-form-item label="新 密 码 ： " prop="password1">
          <el-input style="width: 60%" v-model="dataForm.password1" />
        </el-form-item>
        <el-form-item label="确认密码：" prop="password2">
          <el-input style="width: 60%" v-model="dataForm.password2" />
        </el-form-item>
        <el-form-item label="账    号： " prop="phone">
          <el-input ref="phone" style="width: 60%" v-model="dataForm.phone" />
        </el-form-item>
        <el-form-item label="验 证 码 ： " prop="code">
          <el-input style="width: 40%" v-model="dataForm.code" />
          <span class="getCode" v-if="!isNext" @click="getcode">获取验证码</span>
          <span class="getCode" v-else-if="isNext && time > 0">{{ time }}秒后重发</span>
          <span class="getCode" v-else @click="sendYanzheng">重新发送</span>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="qxyzm">取消</el-button>
        <el-button type="primary" @click="modifypass">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { isPassword, isMobile } from '@/utils/validate'
import { sendmsg, resetpassword } from '@/api/login.js'
import { getToken } from '@/utils/auth'
export default {
  name: 'Login',
  data() {
    var validatePass = (rule, value, callback) => {
      // console.log(value === '' || value === undefined)
      if (value === '' || value === undefined) {
        callback(new Error('请输入密码'))
      } else if (value && !isPassword(value)) {
        callback(new Error('密码6~20位 必须含大写字母、小写字母、数字'))
      } else {
        if (this.dataForm.password1 !== '') {
          this.$refs.dataForm.validateField('password2')
        }
        callback()
      }
    }
    var validatePass2 = (rule, value, callback) => {
      if (value === '' || value === undefined) {
        callback(new Error('请再次输入密码'))
      } else if (value && !isPassword(value)) {
        callback(new Error('密码6~20位 必须含大写字母、小写字母、数字'))
      } else if (value !== this.dataForm.password1) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('密码长度大于6'))
      } else {
        callback()
      }
    }
    const validatePasswordT = (rule, value, callback) => {
      if (value && !isPassword(value)) {
        callback(new Error('密码6~20位 必须含大写字母、小写字母、数字'))
      } else {
        callback()
      }
    }
    return {
      timer: undefined, // 倒计时全局删除
      isNext: false,
      time: 90, // 发送短信时间,倒计时90s
      dataForm: {
        password1: undefined,
        password2: undefined,
        phone: undefined,
        code: undefined,
      },
      rules: {
        password1: [
          {
            required: true,
            validator: validatePass,
            trigger: 'blur',
          },
        ],
        password2: [
          {
            required: true,
            validator: validatePass2,
            trigger: 'blur',
          },
        ],
        phone: [{ required: true, message: '请输入账号', trigger: 'blur' }],
        code: [{ required: true, message: '请输入验证码', trigger: 'blur' }],
      },
      dialogFormVisible: false, // 修改密码弹框控制
      loginForm: {
        account: '',
        password: '',
      },
      loginRules: {
        account: [{ required: true, message: '登录账号不能为空', trigger: 'blur' }],
        // 暂时去掉登录密码限制
        // password: [{ required: true, trigger: 'blur', validator: validatePasswordT }],
        password: [{ required: true, trigger: 'blur', message: '请输入密码' }],
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined,
    }
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true,
    },
  },
  methods: {
    qxyzm() {
      this.dialogFormVisible = false
      this.time = 90
      this.isNext = false
      clearInterval(this.timer)
      this.$refs.dataForm.resetFields()
    },
    getcode() {
      this.sendYanzheng()
      // if (isMobile(this.dataForm.phone)) {
      //   this.sendYanzheng()
      // } else {
      //   this.dataForm.phone = ''
      //   this.$refs.phone.focus()
      //   this.$message({
      //     message: '请输入正确手机号',
      //     type: 'warning'
      //   })
      // }
    },
    sendYanzheng() {
      var that = this
      sendmsg({ account: this.dataForm.phone }).then((res) => {
        if (res.code == 200) {
          this.$notify.success({
            title: '成功',
            message: '验证码发送成功请注意查收',
          })
          that.isNext = true
          that.time = 90
          that.timer = setInterval(() => {
            if (that.time > 0) {
              that.time -= 1
            } else {
              clearInterval(timer)
            }
          }, 1000)
        }
      })
    },
    modifypass() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          var that = this
          resetpassword({
            phone: that.dataForm.phone,
            code: that.dataForm.code,
            pwd: that.dataForm.password1,
          })
            .then((res) => {
              if (res.code == 200) {
                that.qxyzm()
                this.$notify.success({
                  title: '成功',
                  message: '密码重置成功',
                })
              }
            })
            .catch((response) => {
              this.$notify.error({
                title: '失败',
                message: response.data.errmsg,
              })
            })
        }
      })
    },
    handleClick() {
      this.loginForm.account = ''
      this.loginForm.password = ''
    },
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true
          this.$store
            .dispatch('user/login', this.loginForm)
            .then((res) => {
              if (getToken()) {
                this.$router.push({ path: this.redirect || '/' })
              }
              this.loading = false
            })
            .catch(() => {
              this.loading = false
            })
        } else {
          // console.log('error submit!!')
          return false
        }
      })
    },
  },
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #283443;
$light_gray: #777777;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .changestyle .el-input input {
    /*      color: $cursor;*/
    color: navajowhite;
  }
}

/* reset element-ui css */
.login-container {
  .changestyle {
    border: 1px solid rgb(192, 192, 192);
    /*background: rgba(0, 0, 0, 0.1);*/
    border-radius: 5px;
    color: #454545;
    .el-input {
      display: inline-block;
      height: 47px;
      width: 85%;

      input {
        background: transparent;
        border: 0px;
        -webkit-appearance: none;
        border-radius: 0px;
        padding: 12px 5px 12px 15px;
        /*   color: $light_gray;*/
        color: $light_gray;
        height: 47px;
        caret-color: $light_gray;

        &:-webkit-autofill {
          /*  box-shadow: 0 0 0px 1000px $bg inset !important;
              -webkit-text-fill-color: $cursor !important;*/
          color: #454545;
          box-shadow: 0 0 0px 1000px white inset !important;
          -webkit-text-fill-color: #454545;
        }
      }
    }
  }
}
</style>

<style lang="scss" scoped>
$bg: #777777;
$dark_gray: #889aa4;
$light_gray: #eee;

.login-container {
  .getCode {
    width: 100px;
    height: 40px;
    line-height: 40px;
    text-align: center;
    display: inline-block;
    color: #fff;
    background-color: #5db42c;
    cursor: pointer;
    box-sizing: border-box;
    font-size: 14px;
    border-radius: 4px;
    margin-left: 20px;
  }
  .findpassstyle {
    text-align: right;
    font-size: 14px;
  }
  .el-tabs__item.is-active {
    border: 2px solid #5db42c;
  }

  min-height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;

  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 160px 35px 0;
    margin: 0 auto;
    overflow: hidden;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      /*color: $light_gray;*/
      color: #8e8e8e;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
</style>
